package com.monitor.controller;

import lombok.extern.slf4j.Slf4j;
import com.monitor.model.AccessLog;
import com.monitor.model.ElasticSearchOverView;
import com.monitor.model.ElasticSearchParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.monitor.service.ElasticSearchClientService;

import java.util.List;


@RestController
@RequestMapping("/search")
@Slf4j
public class ElasticSearchClientController {
    @Autowired
    private ElasticSearchClientService elasticSearchClientService;

    @PostMapping("/creatIndex")
    public ResponseEntity createIndex(@RequestBody ElasticSearchParams params) {
        boolean result = elasticSearchClientService.createIndex(params);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @PostMapping("/createDoc")
    public ResponseEntity creatDoc(@RequestBody ElasticSearchParams params) {
        String result = elasticSearchClientService.createDoc(params);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @PostMapping("/deleteIndex")
    public ResponseEntity deleteIndex(@RequestBody ElasticSearchParams params) {
        String result = elasticSearchClientService.deleteIndex(params);
        return new ResponseEntity(result,HttpStatus.OK);
    }



    @PostMapping("/searchInRangeOfRequestTime")
    public ResponseEntity searchInRangeOfRequestTime(@RequestBody ElasticSearchParams params) {
        List<AccessLog> httpLogs = elasticSearchClientService.searchInRangeOfRequestTime(params);
        return new ResponseEntity(httpLogs, HttpStatus.OK);
    }

    @PostMapping("/overview")
    public ResponseEntity overview(@RequestBody ElasticSearchParams params) {

        ElasticSearchOverView elasticSearchOverView = elasticSearchClientService.overview(params);
        return new ResponseEntity(elasticSearchOverView, HttpStatus.OK);
    }
}
